const app = getApp()
Page({
  data: {
    userInfo: {},
  },
  onLoad: function(options) {
    var that = this;
    // app.userLogin();
    //获取用户数据缓存
    wx.getStorage({
      key: 'users',
      success: function(e) {
        app.globalData.userInfo = e.data;
        that.setData({
          userInfo: e.data
        });
      },
      fail(e) {
        console.log('失败', e.errMsg)
        wx.showToast({
          title: '请登录后在修改',
          duration: 2000,
          success: function(e) {
            wx.reLaunch({
              url: '../user/user'
            })
          }
        })
      }
    })
  },
  submitdate: function(event) {
    var that = this;
    var userInfo = event.detail.value;
    
    if (userInfo.phone.length != 11) {
      wx.showToast({
        title: '手机号长度有误！',
        icon: 'none',
        mask: true,
        duration: 1500
      })
      return false;
    }

    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    if (!myreg.test(userInfo.phone)) {
      wx.showToast({
        title: '手机号有误！',
        icon: 'none',
        mask: true,
        duration: 1500
      })
      return false;
    }

    if (userInfo.storeId == '' || userInfo.name == '' || userInfo.phone == '') {
      wx.showToast({
        title: '请输入信息',
        icon: 'none',
        mask: true,
        duration: 1500
      })
    } else {
      userInfo.openId = app.openId;
      // 校验
      app.request({
        url: "/wxMatching",
        data: userInfo,
        success: function(e) {
          console.log('e--',e);
          app.globalData.userInfo = userInfo;
          that.setData({
            globalData: app.globalData
          })
          wx.hideLoading()
          wx.setStorage({
            key: 'users',
            data: userInfo,
            success: function(e) {
              wx.showToast({
                title: '修改成功',
                icon: 'success',
                mask: true,
                duration: 2000
              })
              setTimeout(function(){
                wx.navigateTo({
                  url: '../index/index'
                })
              },2000)
            }
          })
        },fail:function(e){
          console.log('e',e)
        }
      })
    }
  },
})